<!--
  - @Description 登录页面
  - @Date 2023/5/19 周五 20:15
  - @Author 酷奇 <vx：17685112557>
-->
<template>
  <div class="login-container">
    <el-row>
      <el-col :span="12" :xs="0" />
      <el-col :span="12" :xs="24">
        <!-- 登录的表单 -->
        <el-form
          ref="loginForms"
          :model="loginForm"
          :rules="rules"
          class="login-form"
        >
          <h1>Hi，老板</h1>
          <h2>欢迎回家！</h2>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              :prefix-icon="User"
              autofocus
              maxlength="8"
              show-word-limit
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              :prefix-icon="Lock"
              maxlength="16"
              show-password
              type="password"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              class="login-btn"
              size="default"
              type="primary"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { ElNotification } from 'element-plus'
import { useRoute, useRouter } from 'vue-router'
import { reactive, ref } from 'vue'
import { Lock, User } from '@element-plus/icons-vue'
import useUserStore from '@/store/modules/user'
import getTime from '@/utils/time'
import { UserLoginVo } from '@/service/user/vo'

const userStore = useUserStore()
const loading = ref(false)
const $route = useRoute()
const $router = useRouter()
const timeInfo = getTime()
const loginForms = ref()

//收集账号与密码的数据
let loginForm: UserLoginVo = reactive({ username: '', password: '' })
const login = async () => {
  await loginForms.value.validate()
  loading.value = true
  try {
    //保证登录成功，可以 .then()；可以try-catch
    await userStore.userLogin(loginForm)
    ElNotification.success({
      title: `${timeInfo}好，老板`,
      message: '登录成功 ！',
    })
    const redirect: any = $route.query.redirect
    // 退出登录后重新登录，需要定位到退出登录时的位置
    // 第一次登录成功跳转到首页
    await $router.push({ path: redirect || '/' })
    loading.value = false
    //判断登录的时候,路由路径当中是否有query参数，如果有就往query参数跳转，没有跳转到首页
  } catch (error) {
    ElNotification.error({
      title: '错误',
      message: '登录失败',
    })
    loading.value = false
  }
}
//自定义校验规则函数
// const validatorUserName = ( rule : any, value : any, callback : any ) => {
//   //rule:即为校验规则对象
//   //value:即为表单元素文本内容
//   //函数:如果符合条件callBack放行通过即为
//   //如果不符合条件callBack方法,注入错误提示信息
//   if( /^\d{4,8}$/.test(value) ) {
//     callback()
//   } else {
//     callback(new Error('账号长度为4~8位'))
//   }
// }

const validatorPassword = (rule: any, value: any, callback: any) => {
  if (/^\d{4,16}$/.test(value)) {
    callback()
  } else {
    callback(new Error('密码长度为4~16位'))
  }
}

//定义表单校验需要配置对象
const rules = {
  username: [
    {
      required: true,
      min: 4,
      max: 8,
      message: '账号长度为4~8位',
      trigger: 'change',
    },
    // { trigger: 'change', validator: validatorUserName },
  ],
  password: [
    // { required: true, min: 4, max: 16, message: '密码长度至少6位', trigger: 'change' }
    { trigger: 'change', validator: validatorPassword },
  ],
}
</script>

<style lang="scss" scoped>
.login-container {
  width: 100%;
  height: $vh100;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login-form {
    position: relative;
    width: 60%;
    top: 30vh;
    padding: 40px;

    &::before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      background: url('@/assets/images/login_form.png') no-repeat;
      background-size: cover;
    }

    h1 {
      color: #ffffff;
      font-size: 40px;
    }

    h2 {
      font-size: 20px;
      color: #ffffff;
      margin: 20px 0;
    }

    .login-btn {
      width: 100%;
    }
  }
}
</style>
